home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / intsdkss.lha / examples / udp / udp_server.c < prev   
C/C++ Source or Header  |  1996-04-09  |  2KB  |  102 lines

  1. /*
  2. ** UDP server example program.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <netdb.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <sys/socket.h>
  13. #include <proto/socket.h>
  14.  
  15. #define DEFAULT_PORT 54321
  16. #define TERMINATOR "quit\n"
  17.  
  18. main ( argc, argv )
  19.      int argc;
  20.      char *argv[];
  21. {
  22. int s, retcode,  done;
  23. long fromlen;
  24. char  buf[256];
  25. struct sockaddr_in addr, from;
  26. struct servent *sp;
  27.  
  28. /*
  29. ** Create a socket.
  30. */
  31. s = socket ( AF_INET, SOCK_DGRAM,0 );
  32. if ( s == -1 ) {
  33.     fprintf ( stderr, "Server can't create socket.\n");
  34.     exit(1);
  35. }
  36.  
  37. /*
  38. ** Applications should get the service number from the services file.
  39. ** Try the services file first, and upon failure, use a default port
  40. ** number.  This allows the example to run without modifying the
  41. ** system environment.
  42. */
  43. memset(&addr, 0, sizeof(addr));
  44. sp = getservbyname( "example", "udp" );
  45. if( sp != NULL ) {
  46.     addr.sin_port = sp->s_port;
  47. } else {
  48.     addr.sin_port = DEFAULT_PORT;
  49. }
  50.  
  51. /*
  52. ** Binding assigns the local port number to the socket.
  53. ** Clients will send datagrams to this port.
  54. ** The address INADDR_ANY means the server is willing to accept
  55. ** datagrams arriving on any local interface.
  56. */
  57. addr.sin_family = AF_INET;
  58. addr.sin_addr.s_addr = INADDR_ANY;
  59. if ( bind ( s, (struct sockaddr *)&addr, sizeof (addr)) == -1 ) {
  60.     perror("bind");
  61.     exit (1);
  62. }
  63.  
  64. fromlen = sizeof(from);
  65.  
  66. /*
  67. ** Server Main Loop - read datagram and echo back to client.
  68. ** If the datagram starts with the word "quit" terminate service.
  69. ** Unlike TCP sockets, UDP will transmit all or none of the
  70. ** requested data so there is no need to worry about partial
  71. ** writes.  However, UDP is an unreliable protocol; datagrams
  72. ** may get lost, duplicated, or delivered in a out of order.
  73. */
  74.  
  75. done = 0;
  76. while (!done) {
  77.     memset(&from, 0, sizeof(from));
  78.     retcode = recvfrom ( s, buf, sizeof(buf), 0,
  79.                 (struct sockaddr *)&from, &fromlen );
  80.     if ( retcode < 0 ) {
  81.     perror("recvfrom");
  82.     exit (1);
  83.     }
  84.     buf[retcode] = 0;
  85.  
  86.     fprintf(stdout, "Packet from %s, port %d, length %3d, data %s",
  87.             inet_ntoa(from.sin_addr),from.sin_port, retcode, buf);
  88.  
  89.     if (!strncmp(buf, TERMINATOR, strlen(TERMINATOR))){
  90.     done = 1;
  91.     }
  92.     retcode = sendto ( s, buf, retcode, 0,
  93.                 (struct sockaddr *)&from, fromlen );
  94.  
  95.     if ( retcode < 0 ) {
  96.     perror("sendto");
  97.     exit (1);
  98.     }
  99. }
  100. exit(0);
  101. }
  102.